package cn.rulian.base.basis.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.rulian.base.basis.bo.EnumManager;
import cn.rulian.base.basis.service.EnumManagerService;
import cn.rulian.base.basis.vo.EnumManagerVo;
import cn.rulian.base.common.dto.Message;
import cn.rulian.base.common.util.xx;
import cn.rulian.base.common.vo.Result;

/**
 * 公共类型-管理器
 */

@Controller
@RequestMapping("/base/basis/enumManager")
public class EnumManagerController {
	
	@Autowired
	private EnumManagerService service;
	
	private final String path="base/basis/enumManager_"; //页面基本路径

	/**
	 * 页面基本路径
	 */
	private String getPath()
	{
		return path;
	}
	
	
	/**
	 * 列表页面
	 */
	@RequestMapping("/listPage")
	public String listPage() {
		return this.getPath()+"list";
	}

	/**
	 * 查询
	 */
	@RequestMapping("/listData")
	@ResponseBody
	public Result listData(EnumManagerVo vo) {
		return service.query(vo);
	}


	/**
	 * 增加页面
	 */
	@RequestMapping("/addPage")
	public String addPage() {
		return this.getPath()+"add";
	}

	/**
	 * 增加
	 */
	@RequestMapping("/add")
	@ResponseBody
	public Message add(EnumManagerVo vo) {
		Message rm = new Message();
		EnumManager bo = vo.getObj();
		
		//验证typeKey是否重复
		List<EnumManager> ls = service.findByTypeKey(bo.getKey());
		if(ls.size()>0)
		{
			rm.setMsg("存在重复的【类型Key】！");
			rm.setCode(Message._ERROR);
			return rm;
		}
		
		try {
			service.save(bo);
			rm.setMsg("保存成功！");
			rm.setCode(Message._SUCCESS);
		} catch (Exception e) {
			rm.setMsg("保存失败，异常信息：<br/>" + e.toString());
			rm.setCode(Message._ERROR);
			e.printStackTrace();
		}
		return rm;
	}

	/**
	 * 修改页面
	 */
	@RequestMapping("/updatePage")
	public String updatePage(Model model, Long id) {
		model.addAttribute("bo", service.findOne(id));
		return this.getPath()+"update";
	}

	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@ResponseBody
	public Message update(EnumManagerVo vo,Long id) {
		Message rm = new Message();
		try {
			EnumManager obj = vo.getObj();
			EnumManager bo = service.findOne(id);
			
			//验证typeKey是否重复
			if(!bo.getKey().equals(obj.getKey()))
			{
				List<EnumManager> ls = service.findByTypeKey(obj.getKey());
				if(ls.size()>0)
				{
					rm.setMsg("存在重复的【类型Key】！");
					rm.setCode(Message._ERROR);
					return rm;
				}
			}
			
			xx.copyObjectField(obj, bo); //属性值copy
			service.save(bo);
			
			rm.setMsg("保存成功！");
			rm.setCode(Message._SUCCESS);
		} catch (Exception e) {
			rm.setMsg("保存失败，异常信息：<br/>" + e.toString());
			rm.setCode(Message._ERROR);
			e.printStackTrace();
		}
		return rm;
	}

	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@ResponseBody
	public Message delete(Long ids[]) {
		Message rm = new Message();
		try {
			for(Long id:ids)
			{
				service.delete(id);
			}
			
			rm.setMsg("删除成功！");
			rm.setCode(Message._SUCCESS);
		} catch (Exception e) {
			rm.setMsg("删除失败，异常信息：<br/>" + e.toString());
			rm.setCode(Message._ERROR);
			e.printStackTrace();
		}
		return rm;
	}
	
	/**
	 * 详情页面
	 */
	@RequestMapping("/detailPage")
	public String detailPage(Model model, Long id) {
		model.addAttribute("bo", service.findOne(id));
		return this.getPath()+"detail";
	}

}
